from random import random
import copy
from blackbox import Point
from blackbox import BlackBox

class HillClimbing:
	def modify(self, pointsList, radius):
		newPoints = [Point(0,0)]
		for point in pointsList[1:-1]:
			newPoints.append(Point(point.x,point.y+random()*2*radius-radius))
		newPoints.append(Point(10,10))
		return newPoints

	def optimize(self, pointsList):
		initialFitness = BlackBox().calculateTime(pointsList)
		bestFitness = initialFitness
		bestSolution = pointsList
		it = 1
		changeMade = True
		while changeMade :
			#print("Big Iteration: " + str(it))
			#print("Best solution: " + str(bestFitness))
			it+=1
			changeMade = False
			for i in range(0,1000):
				newPoints = self.modify(bestSolution,0.2)
				curFitness = BlackBox().calculateTime(newPoints)
				if curFitness < bestFitness:
					bestFitness = curFitness
					bestSolution = newPoints
					changeMade=True
		it=1
		changeMade = True
		while changeMade :
			#print("Small Iteration: " + str(it))
			#print("Best solution: " + str(bestFitness))
			it+=1
			changeMade = False
			for i in range(0,1000):
				newPoints = self.modify(bestSolution,0.01)
				curFitness = BlackBox().calculateTime(newPoints)
				if curFitness < bestFitness:
					bestFitness = curFitness
					bestSolution = newPoints
					changeMade=True
		return bestSolution

		